package com.qf.proxy.util.http.ge.httpclient;

import cn.hutool.core.util.StrUtil;
import com.qf.proxy.util.SmallTool;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicCredentialsProvider;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author claud
 * 2018年8月15日17:47:52
 * httpclient的所需参数类
 */
@Data
public class HttpClientConfig {


    public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54";
    /**
     * 默认的超时时间--wos的一般时间都很长，需要设置为25000
     */
    public static int TIME_OUT = 9800;


    private static Integer bdIpIndex = 0;
    /**
     * Post请求字符串
     */
    String postDate;
    /**
     * Post请求参数
     */
    List<NameValuePair> nvpsList = new ArrayList<>();
    /**
     * 默认Cookie解析形式
     */
    private String cookieSpecs = "default";
    /**
     * 默认出口IP
     */
    private String localIp = "";
    /**
     * 默认是否自动跳转
     */
    private Boolean autoRedirect = true;
    /**
     * 默认允许跳转次数
     */
    private Integer maxRedirects = 50;
    /**
     * 默认代理账号
     */
    private String proxyUser = "";
    /**
     * 默认代理密码
     */
    private String proxyPass = "";

    private String postJsonStr = "";
    /**
     * 请求配置
     */
    private RequestConfig requestConfig;
    /**
     * 代理请求配置
     */
    private CredentialsProvider credsProvider;
    private Map<String, String> headerMap = new HashMap<>();
    private String refer;
    private String host;
    private String accept;

    //post map 组合的数据
    private Map<String, String> postMap;

    /**
     *
     */
    private String Connection = "keep-alive";

    private String proxyIp = "127.0.0.1";
    private Integer proxyPort = UrlEntity.port;

    private ProxyInfo proxyInfo;

    // 开启外网代理
    private boolean isProxy;
    private Boolean sslUse = true;
    /**
     * 存储空间路径
     */
    private String storagePath;
    //存储路径
    private String dirPath = UrlEntity.PathHead + "/AV/temp/";
    //存储文件名
    private String filePath;

    private BasicCookieStore cookieStore = new BasicCookieStore();
    /**
     * 特殊cookie
     */
    private String cookie;
    /**
     * 需要跳转的地址
     */
    private String url;

    /**
     * 允许原地址重定向
     */
    private Boolean circularRedirect = false;


//    public HttpClientConfig() {
//        super();
//        this.requestConfig = createRequestConfigure(null, null, null, TIME_OUT, true);
//    }


    /**
     * 生成代理ip的请求配置的方法
     * ConnectionTimeout是tcp连接建立的时间--即tcp三次握手完成时间.
     * SocketTimeout是连接建立后,数据传输过程中数据报之间间隔的最大时间.
     * localAdd是多ip的主机的不同出口ip
     */
    public static RequestConfig createRequestConfigure(String remoteIp, Integer remotePort, String localAdd, int timeOut, Boolean redirectFlag) {
        HttpHost host = null;
        if (!StringUtils.isEmpty(remoteIp)) {
            host = new HttpHost(remoteIp, remotePort);
        }
        InetAddress adds = null;
        if (localAdd != null) {
            try {
                adds = InetAddress.getByName(localAdd);
            } catch (UnknownHostException e) {
                SmallTool.printMessError("异常信息++" + e.getMessage());
            }
        }
        RequestConfig requestConfig = RequestConfig.custom()
//                .setCookieSpec(CookieSpecs.STANDARD)
                .setSocketTimeout(timeOut)
                .setConnectTimeout(timeOut)
                .setRedirectsEnabled(redirectFlag)
                .setProxy(host)
                .setLocalAddress(adds)
                .build();
        return requestConfig;
    }

    public boolean isProxy() {
        return isProxy;
    }

    public void setProxy(boolean proxy) {
        isProxy = proxy;
    }


    public HttpClientConfig build() {

        // 这里只设置ip 信息 至于那个有密码的代理是在httpclient 里面设置的
        InetAddress adds = null;
        if (StrUtil.isNotEmpty(localIp)) {
            try {
                adds = InetAddress.getByName(localIp);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }

        HttpHost proxy = null;

        boolean isProxySet = false;
        if (proxyInfo != null) {
            credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(proxyInfo.getProxyIp(), Integer.parseInt(proxyInfo.getProxyPort())), new UsernamePasswordCredentials(proxyInfo.getUserName(), proxyInfo.getPassword()));
            proxy = new HttpHost(proxyInfo.getProxyIp(), Integer.parseInt(proxyInfo.getProxyPort()));
            isProxySet = true;
        }

        if (!isProxySet && StrUtil.isNotEmpty(proxyIp) && proxyPort > 0 && isProxy) {
            proxy = new HttpHost(proxyIp, proxyPort);
        }

        requestConfig = RequestConfig.custom()
                .setCookieSpec(cookieSpecs)
                .setSocketTimeout(TIME_OUT)
                .setConnectTimeout(TIME_OUT)
                .setConnectionRequestTimeout(TIME_OUT)
                .setRedirectsEnabled(autoRedirect)
                .setCircularRedirectsAllowed(circularRedirect)
                .setProxy(proxy)
                .setLocalAddress(adds)
                .setMaxRedirects(maxRedirects)
                .build();

        return this;
    }

}
